/**
 * 
 */
package com.huarui.desktop.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.huarui.desktop.dao.DesktopDao;
import com.huarui.desktop.dao.DesktopJsModuleDao;
import com.huarui.desktop.dao.DesktopMenuDao;
import com.huarui.desktop.model.Desktop;
import com.huarui.desktop.model.DesktopJsModule;
import com.huarui.desktop.model.DesktopMenu;
import com.huarui.desktop.service.DesktopService;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.security.UserHolder;

/**
 * @author peng.sunp
 * 
 */
public class DefaultDesktopService implements DesktopService {

	private DesktopDao desktopDao;
	private DesktopMenuDao desktopMenuDao;
	public DesktopJsModuleDao desktopJsModuleDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.huarui.desktop.service.DesktopService#getOwnDesktop()
	 */
	@Override
	public Desktop getOwnDesktop() {
		SessionMessage sessionMessage = UserHolder.getCurrentSessionMessage();
		Integer companyType = sessionMessage.getCompanyType();
		Integer employeeId = sessionMessage.getEmployeeId();
		Integer desktopTarget = (employeeId == null || employeeId == 0) ? Desktop.DESKTOP_TARGET_COMMON_USER : Desktop.DESKTOP_TARGET_COMPANY;
		return this.desktopDao.selectDesktopByCompanyType(companyType, desktopTarget);
	}

	/*
	 * (non-Javadoc)
	 * @see com.huarui.desktop.service.DesktopService#getOwnPermissionIds()
	 */
	public List<Integer> getOwnPermissionIds(){
		Integer employeeId=UserHolder.getCurrentSessionMessage().getEmployeeId();
		return this.desktopJsModuleDao.selectPermissionId4User(employeeId==null?0:employeeId);
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.huarui.desktop.service.DesktopService#getOwnDesktopJsModules(java
	 * .lang.Integer, java.util.Collection)
	 */
	@Override
	public List<DesktopJsModule> getOwnDesktopJsModules(Integer desktopId, Collection<DesktopMenu> desktopMenus) {
		SessionMessage sessionMessage = UserHolder.getCurrentSessionMessage();
		Integer employeeId = sessionMessage.getEmployeeId();
		List<DesktopJsModule> desktopJsModules = this.desktopJsModuleDao.selectDesktopJsModules4User(desktopId, employeeId == null ? 0 : employeeId);
		Map<Integer, ArrayList<Integer>> jsModuleMenus = new HashMap<Integer, ArrayList<Integer>>();
		for (DesktopMenu menu : desktopMenus) {
			if (menu.getMenuType() == DesktopMenu.MENU_TYPE_MODULE) {
				Integer moduleId = menu.getJsModuleId();
				if (moduleId != null) {
					ArrayList<Integer> menuIds = jsModuleMenus.get(moduleId);
					if (menuIds == null) {
						menuIds = new ArrayList<Integer>();
						jsModuleMenus.put(moduleId, menuIds);
					}
					menuIds.add(menu.getId());
				}
			}
		}
		for (DesktopJsModule jsModule : desktopJsModules) {
			ArrayList<Integer> menuIds = jsModuleMenus.get(jsModule.getJsModuleId());
			if (menuIds != null) {
				jsModule.setMenuIds(menuIds);
			}
		}
		return desktopJsModules;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.huarui.desktop.service.DesktopService#getDesktopMenus(java.lang.Integer
	 * )
	 */
	@Override
	public Map<Integer, DesktopMenu> getDesktopMenus(Integer desktopId) {
		return this.desktopMenuDao.selectDesktopMenus(desktopId);
	}

	public void setDesktopDao(DesktopDao desktopDao) {
		this.desktopDao = desktopDao;
	}

	public void setDesktopMenuDao(DesktopMenuDao desktopMenuDao) {
		this.desktopMenuDao = desktopMenuDao;
	}

	public void setDesktopJsModuleDao(DesktopJsModuleDao desktopJsModuleDao) {
		this.desktopJsModuleDao = desktopJsModuleDao;
	}

}
